import 'package:flutter/material.dart';
import 'package:xindong/common/xindong_define.dart';


// 通话的拖动视图
class XinDongDragContainer extends StatefulWidget {
  final Widget child; // 子视图
  final double width; // 容器宽
  final double height; // 容器高
  final double spaceLeftRight; // 保持间距：左右
  final double? offsetTop; // 默认偏移：顶部
  final double? offsetRight; // 默认偏移：右部
  final bool disableMoveHorizontal; // 禁止移动：水平方向
  final bool disableMoveVertical; // 禁止移动：垂直方向

  const XinDongDragContainer({
    Key? key,
    required this.child,
    this.width = 100,
    this.height = 130,
    this.offsetTop,
    this.offsetRight,
    this.spaceLeftRight = 15,
    this.disableMoveHorizontal = false,
    this.disableMoveVertical = false,
  }) : super(key: key);

  @override
  State<XinDongDragContainer> createState() => _XinDongDragContainerState();
}

class _XinDongDragContainerState extends State<XinDongDragContainer> {
  static final double _topSpace = statusBarHeight + 44;
  double _viewTop = 40 + statusBarHeight + 44;
  double _viewRight = 0;

  @override
  void initState() {
    super.initState();
    // 左右保持间距
    _viewRight = widget.spaceLeftRight;
    // 默认偏移：顶部
    if (widget.offsetTop != null) {
      _viewTop = widget.offsetTop!;
    }
    // 默认偏移：右部
    if (widget.offsetRight != null) {
      _viewRight = widget.offsetRight!;
    }
  }

  safeSetState(callBack) {
    setState(() {
      if (mounted) {
        callBack();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Positioned(
          top: _viewTop,
          right: _viewRight,
          width: widget.width,
          height: widget.height,
          child: GestureDetector(
            onPanUpdate: (DragUpdateDetails e) {
              // 用户手指滑动时，更新偏移，重新构建
              safeSetState(() {
                // 禁止移动
                if (!widget.disableMoveHorizontal) {
                  _viewRight -= e.delta.dx;
                }
                if (!widget.disableMoveVertical) {
                  _viewTop += e.delta.dy;
                }
                // 越界判断
                if (_viewRight < widget.spaceLeftRight) {
                  _viewRight = widget.spaceLeftRight;
                } else if (_viewRight > screenWidth - widget.width - widget.spaceLeftRight) {
                  _viewRight = screenWidth - widget.width - widget.spaceLeftRight;
                }
                if (_viewTop < _topSpace) {
                  _viewTop = _topSpace;
                } else if (_viewTop > screenHeight - widget.height - 49 - bottomBarHeight) {
                  _viewTop = screenHeight - widget.height - 49 - bottomBarHeight;
                }
              });
            },
            onPanEnd: (DragEndDetails e) {
              safeSetState(() {
                if (_viewRight < (screenWidth - widget.width) / 2) {
                  _viewRight = widget.spaceLeftRight;
                } else {
                  _viewRight = screenWidth - widget.width - widget.spaceLeftRight;
                }
              });
            },
            child: widget.child,
          ),
        ),
      ],
    );
  }
}
